#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=settings.ico
#AutoIt3Wrapper_outfile=killbits-event.exe
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Res_Comment=ActiveX Kill Bits Browser
#AutoIt3Wrapper_Res_Description=ActiveX Kill Bits Browser
#AutoIt3Wrapper_Res_Fileversion=1.1.0.1
#AutoIt3Wrapper_Res_LegalCopyright=(c) 2011 Dave Fulgham
#AutoIt3Wrapper_Res_Language=1033
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIListView.au3>
Dim $ourCLSID, $ourWalk, $ourLine
Dim $nCol, $nColumn, $nCtrlID, $nCurCol, $nIndex, $nItem1, $nItem2, $nItemID, $bSet, $nSortDir, $nColAdjust, $ourButton
Dim $ourMatches, $verifyMsg
Dim $changesSize = 16
Dim $changes[$changesSize][3]
Dim $changeTop = 0

Opt("GUIOnEventMode", 1)

#Region ### START Koda GUI section ### Form=c:\users\dave\desktop\koda\forms\killbits.kxf
$Form1_1 = GUICreate("Killbits Browser", 444, 235, 192, 124, BitOR($GUI_SS_DEFAULT_GUI,$WS_SIZEBOX,$WS_THICKFRAME))
$ListView1 = GUICtrlCreateListView("|Control|CLSID|Cheat", 0, 0, 441, 209, -1, BitOR($WS_EX_CLIENTEDGE,$LVS_EX_CHECKBOXES, $LVS_SORTASCENDING))
GUICtrlSetOnEvent($ListView1, "ListViewClick")
GUICtrlRegisterListViewSort(-1, "LVSort")
$nCol = 5

GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")
GUIRegisterMsg($WM_GETMINMAXINFO, "MY_WM_GETMINMAXINFO")


While 1
	$ourWalk = $ourWalk +1
	$ourCLSID = RegEnumKey("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\ActiveX Compatibility", $ourWalk)
	If @error Then ExitLoop
	$friendlyName = RegRead("HKEY_CLASSES_ROOT\CLSID\" & $ourCLSID, "")
	If $friendlyName Then
		$ourLine = GUICtrlCreateListViewItem("|" & $friendlyName & "|" & $ourCLSID, $ListView1)
		GUICtrlSetOnEvent($ourLine, "ListItemClick")
		If (BitAND(RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\ActiveX Compatibility\" & $ourCLSID, "Compatibility Flags"), 0x400) ) Then
			GUICtrlSetState($ourLine, $GUI_CHECKED)
			GUICtrlSetData($ourLine, "|||a")
		EndIf
	EndIf
WEnd

$ourButton = GUICtrlCreateButton("Commit",0, 210)
GUICtrlSetOnEvent($ourButton, "CommitClick")
GUICtrlSetState($ourButton, $GUI_DISABLE)

;MsgBox(0, "Debug - Congtrols count:", $ourWalk)

_GUICtrlListView_SetColumnWidth($ListView1, 3, 0)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $ListView1
			$bSet = 0
			$nCurCol = $nCol

	EndSwitch
WEnd

; Our sorting callback funtion
Func LVSort($hWnd, $nItem1, $nItem2, $nColumn)
    Local $nSort, $val1, $val2, $nResult

    ; Switch the sorting direction
    If $nColumn = $nCurCol Then
        If Not $bSet Then
			;MsgBox(0, "Debug - nResult:", "Flip sort dir")
            $nSortDir = $nSortDir * -1
            $bSet = 1
        EndIf
    Else
        $nSortDir = 1
		If $nColumn = 0 Then
			$nColAdjust = 3
		Else
			$nColAdjust = 0
		EndIf
    EndIf
	;$nSortDir = $nSortDir * -1
    $nCol = $nColumn

    $val1 = GetSubItemText($hWnd, $nItem1, $nColumn + $nColAdjust)
    $val2 = GetSubItemText($hWnd, $nItem2, $nColumn + $nColAdjust)

    ; If it is the 3rd colum (column starts with 0) then compare the dates
    If $nColumn = 2 Then
        $val1 = StringMid($val1, 2, 8)
        $val2 = StringMid($val2, 2, 8)
    EndIf

    $nResult = 0    ; No change of item1 and item2 positions

    If $val1 < $val2 Then
        $nResult = -1   ; Put item2 before item1
    ElseIf $val1 > $val2 Then
        $nResult = 1    ; Put item2 behind item1
    EndIf

    $nResult = $nResult * $nSortDir
;	MsgBox(0, "Debug - nResult:", $nResult)

    Return $nResult
EndFunc   ;==>LVSort

; Retrieve the text of a listview item in a specified column
Func GetSubItemText($nCtrlID, $nItemID, $nColumn)
    Local $stLvfi = DllStructCreate("uint;ptr;int;int[2];int")
    Local $nIndex, $stBuffer, $stLvi, $sItemText

    DllStructSetData($stLvfi, 1, $LVFI_PARAM)
    DllStructSetData($stLvfi, 3, $nItemID)

    $stBuffer = DllStructCreate("char[260]")

    $nIndex = GUICtrlSendMsg($nCtrlID, $LVM_FINDITEM, -1, DllStructGetPtr($stLvfi));

    $stLvi = DllStructCreate("uint;int;int;uint;uint;ptr;int;int;int;int")

    DllStructSetData($stLvi, 1, $LVIF_TEXT)
    DllStructSetData($stLvi, 2, $nIndex)
    DllStructSetData($stLvi, 3, $nColumn)
    DllStructSetData($stLvi, 6, DllStructGetPtr($stBuffer))
    DllStructSetData($stLvi, 7, 260)

    GUICtrlSendMsg($nCtrlID, $LVM_GETITEMA, 0, DllStructGetPtr($stLvi));

    $sItemText = DllStructGetData($stBuffer, 1)

    $stLvi = 0
    $stLvfi = 0
    $stBuffer = 0

    Return $sItemText
EndFunc   ;==>GetSubItemText

Func CommitClick()
	;MsgBox(0, "Debug", "Array sized is: " & UBound($changes))
	$verifyMsg = "You have made the following changes:" & @CRLF
	For $step = 0 to $changeTop - 1
		$ourMatches = StringRegExp($changes[$step][1], '\|(.*?)\|', 1)
		;MsgBox(0, "Debug", "Array sized is: " & UBound($ourMatches) & @CRLF & $changes[$step][1])
		$verifyMsg = $verifyMsg & "Killbit for " & $ourMatches[0] & " will be set to: "
		If $changes[$step][2] = 1 Then
			$verifyMsg &= "ON (Control will NOT be shown.)" & @CRLF & @CRLF
		Else
			$verifyMsg &= "OFF (Control WILL be shown.)" & @CRLF & @CRLF
		EndIf
	Next
	$verifyMsg &= "Do you wish to commit your changes?"
	$ourAnswer = MsgBox(4, "Confirm:", $verifyMsg)
	If $ourAnswer = 6 Then
		For $step = 0 To $changeTop - 1
			$ourCLSID = StringRegExp($changes[$step][1], '{(.*)}', 1)
			$ourFlags = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\ActiveX Compatibility\{" & $ourCLSID[0] & "}", "Compatibility Flags")
			If $changes[$step][2] = 1 Then
				$ourFlags = BitOR($ourFlags, 0x400)
			Else
				$ourFlags = BitXOR($ourFlags, 0x400)
			EndIf

			;MsgBox(0, "Debug", "We would write: " & $ourFlags & @CRLF & "at: " & "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\ActiveX Compatibility\{" & $ourCLSID[0] & "}")
			$ourAnswer = RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\ActiveX Compatibility\{" & $ourCLSID[0] & "}", "Compatibility Flags", "REG_DWORD", $ourFlags)
			If Not ($ourAnswer = 1) Then
				MsgBox(0, "Error", "Unable to save your changes to the registry. A likely cause for this is insufficient rights. Log in as an administrator and be sure to Run as Administrator on Win 7 or Vista.")
			EndIf
			;MsgBox(0, "Debug", $ourAnswer & @CRLF & @error)
		Next
		GUICtrlSetState($ourButton, $GUI_DISABLE)
	EndIf

EndFunc

Func SpecialEvents()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
            ;MsgBox(0, "Close Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)
            Exit

        Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
            ;MsgBox(0, "Window Minimized", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)

        Case @GUI_CtrlId = $GUI_EVENT_RESTORE
            ;MsgBox(0, "Window Restored", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)

    EndSelect
EndFunc   ;==>SpecialEvents

Func ListViewClick()
	$bSet = 0
	$nCurCol = $nCol
EndFunc

Func ListItemClick()
	;MsgBox(0, "Debug", GUICtrlRead(@GUI_CtrlId, 1) & @CRLF & "$GUI_CHECKED is:" & $GUI_CHECKED)

	If $changeTop > 0 Then
		For $step = 0 To $changeTop - 1
			If $changes[$step][0] = @GUI_CtrlId Then
				$changes[$step][2] = GUICtrlRead(@GUI_CtrlId, 1)
				GUICtrlSetState($ourButton, $GUI_ENABLE)
				Return
			EndIf
		Next
		If $changeTop >= $changesSize Then
			$changesSize = $changesSize + 16
			ReDim $changes[$changesSize][3]
		EndIf
		$changes[$changeTop][0] = @GUI_CtrlId
		$changes[$changeTop][1] = GUICtrlRead(@GUI_CtrlId, 0) ; Text from columns
		$changes[$changeTop][2] = GUICtrlRead(@GUI_CtrlId, 1) ; State of checkbox
		;MsgBox(0, "Debug", "Changetop is: " & $changeTop & " wrote:" & @CRLF & $changes[$changeTop][1])
		$changeTop = $changeTop + 1
	Else
		$changes[0][0] = @GUI_CtrlId
		$changes[0][1] = GUICtrlRead(@GUI_CtrlId, 0) ; Text from columns
		$changes[0][2] = GUICtrlRead(@GUI_CtrlId, 1) ; State of checkbox
		$changeTop = 1
	EndIf
	GUICtrlSetState($ourButton, $GUI_ENABLE)
EndFunc

Func MY_WM_GETMINMAXINFO($hWnd, $Msg, $wParam, $lParam)
    If $hWnd=$Form1_1 Then; the main GUI-limited to 640x480
        $minmaxinfo = DllStructCreate("int;int;int;int;int;int;int;int;int;int",$lParam)
        DllStructSetData($minmaxinfo,7,460); min X
        DllStructSetData($minmaxinfo,8,270); min Y
        Return 0
    Else;other dialogs-"no" lower limit
        Return 0
    EndIf
EndFunc

